package com.itheima.user.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.dto.NotificationSettingsDto;
import com.itheima.entity.pojo.NotificationSettings;
import com.itheima.user.mapper.NotificationSettingsMapper;
import com.itheima.user.service.NotificationSettingsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author :wujunjie
 * createTime :2022-12-21 10:25
 * description :
 */
@Service
public class NotificationSettingsServiceImpl implements NotificationSettingsService {
    @Autowired
    private NotificationSettingsMapper notificationSettingsMapper;

    @Override

    public Boolean insertNotificationSettings(String userId) {
        NotificationSettings notificationSettings = new NotificationSettings();
        notificationSettings.setId(IdUtil.getSnowflakeNextIdStr());
        notificationSettings.setUserId(userId);
        notificationSettings.setCreateTime(LocalDateTime.now());
        Boolean insert=notificationSettingsMapper.insert(notificationSettings);
        return insert;
    }

    @Override
    public Boolean update(NotificationSettingsDto notificationSettingsDto) {
        NotificationSettings notificationSettingsInDb=notificationSettingsMapper.select(ThreadLocalUtil.getId());
        if (ObjectUtil.isEmpty(notificationSettingsInDb)){
            throw new RuntimeException("要修改的通知设置数据不存在");
        }
        NotificationSettings notificationSettings = new NotificationSettings();
        BeanUtils.copyProperties(notificationSettingsDto, notificationSettings);
        notificationSettings.setId(notificationSettingsInDb.getId());
        notificationSettings.setUpdateTime(LocalDateTime.now());
        Boolean update=notificationSettingsMapper.update(notificationSettings);
        return update;
    }

    @Override
    public NotificationSettings select() {

        return notificationSettingsMapper.select(ThreadLocalUtil.getId());
    }
}
